﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace Database
{
    [DataContract]
    public class Forum// : ForumImpl
    {
        [DataMember]
        private int id;
        [DataMember]
        private string title;
        [DataMember]
        private SortedDictionary<int, SubForum> subForums;
        [DataMember]
        private User admin;
        [DataMember]
        private SortedDictionary<string, User> users;
        [DataMember]
        private int idForSubForums;
        [DataMember]
        private int type;


        public Forum(int id, string title, User admin, int type) {
            this.id = id;
            this.title = title;
            this.subForums = new SortedDictionary<int, SubForum>();
            this.admin = admin;
            this.users = new SortedDictionary <string, User>();
            this.users.Add(admin.getUserName(), admin);
            this.type = type;
        }

        public void setIdForSubForums(int num) {
            this.idForSubForums = num;
        }

        public int getId()
        {
            return this.id;
        }

        public string getTitle()
        {
            return this.title;
        }

        public void setTitle(string title)
        {
            this.title = title;
        }

        public int getType()
        {
            return this.type;
        }

        public void setTitle(int type)
        {
            this.type = type;
        }

        public SortedDictionary<int, SubForum> getSubForums() {
            return this.subForums;
        }
        public void setSubForums(SortedDictionary<int, SubForum> subForums)
        {
            this.subForums = subForums;
        }

        //return id of the new SubForum
        public int addSubForum(string title, User admin) {
            idForSubForums += 1;
            SubForum subForum = new SubForum(idForSubForums, title, id, admin);
            subForums.Add(idForSubForums, subForum);
            return idForSubForums;
        }

        public SubForum getSubForum(int subForumId){
            if (subForums.ContainsKey(subForumId))
                return subForums[subForumId];
            else
                return null;
        }

        public bool removeSubForum(int subForumdId){
            return subForums.Remove(subForumdId); ;
        }

        public int getNumOfSubForum(){
            return this.subForums.Count;
        }

        public User getAdmin() {
            return this.admin;
        }

        public SortedDictionary<string, User> getUsers() {
            return this.users;
        }
        public void setUsers(SortedDictionary<string, User> users)
        {
            this.users = users;
        }

        public User registerUser(string uname, string name, string lastName, string password, string email) {
            if (users.ContainsKey(uname))
                return null;
            else {
                User newUser = new User(uname, name, lastName, password, email);
                users.Add(uname, newUser);
                return newUser;
            }
        }
        public User registerUser(User user)
        {
            if (users.ContainsKey(user.getUserName()))
                return null;
            else
            {
                users.Add(user.getUserName(), user);
                return user;
            }
        }

        public bool containsUser(string uname){
            if (uname != null) 
                return users.ContainsKey(uname);
            else
                return false;
        }

        public bool containsUser(User user)
        {
            if (user != null)
                return users.ContainsKey(user.getUserName());
            else
                return false;
        }

        public User getRegUser(string uname)
        {
            if (uname != null && users.ContainsKey(uname))
                return users[uname];
            else
                return null;

        }

        //return SortedDictionary <int, string> : int -> id, string -> the subForums titles
        public SortedDictionary<int, string> forumToString()
        {
                SortedDictionary<int, string> titles = new SortedDictionary<int, string>();
                foreach (int id in this.subForums.Keys)
                {
                    titles.Add(id, this.subForums[id].getTitle());
                }
                return titles;
        }



    }
}
